package btreegui;

import btreetestat.TreeContainer;
import btreetestat.TreeNode;
import java.awt.*;
import java.util.Collection;
import javax.swing.JPanel;

public class DrawTree extends JPanel {

    private DrawGrids grid;
    private boolean drawGrid = false;
    private int depth = 0;
    private int gesamtdepth = 0;
    private BTreeGUI frame;
    private GuiContainer guiContainer;
    private String searchValue = "";

    public DrawTree(BTreeGUI frame) {
        this.frame = frame;
        grid = new DrawGrids(900, 600, 20, 30);
        guiContainer = new GuiContainer(frame);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        setBackground(Color.white);
        //Draw Grid
        if (drawGrid) {
            grid.paint(g);
        }
        //Tree zeichnen
        guiContainer.paintComponent(g2d);
    }

    // Werte für B-Tree GUI auslesen
    public GuiContainer drawTree1(TreeContainer container) {
        GuiContainer guiContainer = new GuiContainer(frame);
        Collection<TreeNode> values = container.values();
        for (TreeNode treeNode : values) {
            String treeval = treeNode.getValue().toString();
            if (treeval.equals(searchValue)) {
                guiContainer.addValue(treeval, Color.yellow, Color.red);        //such element
            } else {
                guiContainer.addValue(treeval, Color.LIGHT_GRAY, Color.BLACK);  //normales element
            }
        }
        if (depth > gesamtdepth) {
            gesamtdepth = depth;
        }
        guiContainer.setActualHight(depth);
        depth++;
        for (TreeNode treeNode : values) {
            if (treeNode.getChildren() != null) {           //Left+Middle Childs
                guiContainer.addChild(drawTree1(treeNode.getChildren()));
            }

            if (treeNode.getBiggerChildren() != null) {     //Right Childs
                guiContainer.addChild(drawTree1(treeNode.getBiggerChildren()));
            }
        }
        depth--;
        return guiContainer;
    }

    public void drawTree(TreeContainer treeContainer) {
        guiContainer = drawTree1(treeContainer);
        guiContainer.setPosition();
        updatePanelSize();
    }

    public void updatePanelSize() {
        try {
            int newWidth = (GuiContainer.offsets[GuiContainer.offsets.length - 1] + 2 * guiContainer.getBucket().getbSize().width);
            if (this.getPreferredSize().width < newWidth) {
                this.setPreferredSize(new Dimension(newWidth, this.getPreferredSize().height));
            }
        } catch (Exception e) {
        }

        //heigth
        int newHeight = (gesamtdepth * 2 * DrawTreeElement.getHeight() + 100);
        if (this.getPreferredSize().height < newHeight) {
            this.setPreferredSize(new Dimension(this.getPreferredSize().width, newHeight));
        }
    }

    public GuiContainer getGuiContainer() {
        return guiContainer;
    }

    public int getDepth() {
        return depth;
    }

    public void setSearchValue(String searchValue) {
        this.searchValue = searchValue;
    }

    public String getSearchValue() {
        return searchValue;
    }
}
